package com.yxw.live_vod_boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_boot.dto.YxVideoCoinAddDTO;
import com.yxw.live_vod_boot.dto.YxVideoCoinExchangeDTO;
import com.yxw.live_vod_boot.entity.YxLiveShopEntity;
import com.yxw.live_vod_boot.entity.YxVideoCoinExchangeEntity;
import com.yxw.live_vod_boot.feign.BillingSetupServiceClient;
import com.yxw.live_vod_boot.mapper.ShopMapper;
import com.yxw.live_vod_boot.mapper.VideoCoinExchangeMapper;
import com.yxw.live_vod_boot.service.VideoCoinExchangeService;
import com.yxw.live_vod_boot.service.VideoCoinService;
import com.yxw.live_vod_boot.vo.YxVideoCoinExchangeVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

/**
 * 视频币兑换
 * @author xiexuxin
 * @date 2023-11-16
 */
@Slf4j
@Service
public class VideoCoinExchangeServiceImpl extends ServiceImpl<VideoCoinExchangeMapper, YxVideoCoinExchangeEntity> implements VideoCoinExchangeService {

    @Autowired
    private VideoCoinExchangeMapper videoCoinExchangeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private VideoCoinService videoCoinService;

    @Autowired
    private BillingSetupServiceClient billingSetupServiceClient;

    /**
     * 兑换视频币
     * @param videoCoinExchangeDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public YxVideoCoinExchangeVO videoCurrencyExchange(YxVideoCoinExchangeDTO videoCoinExchangeDTO) {
        if (videoCoinExchangeDTO == null || videoCoinExchangeDTO.getExchangeVideoCoins() == null) {
            throw new BaseException("请输入需要兑换多少视频币！");
        }
        // 查询店铺的视频币兑换信息
        YxVideoCoinExchangeEntity videoCoinExchange = videoCoinExchangeMapper.selectByExchangeShopId(videoCoinExchangeDTO.getExchangeShopId());
        // 如果店铺第一次兑换视频币,将先添加对应的数据
        if (videoCoinExchange == null) {
            addVideoCoinExchange(videoCoinExchangeDTO);
            videoCoinExchange = videoCoinExchangeMapper.selectByExchangeShopId(videoCoinExchangeDTO.getExchangeShopId());
        }
        // 获取兑换比例
        BaseResult videoCoinRatio = billingSetupServiceClient.getVideoCoinRatio();
        Integer exchangeRatio = 0;
        exchangeRatio = (Integer)videoCoinRatio.getData();
        // 校验兑换比例的合法性
        if (exchangeRatio <= 0) {
            throw new BaseException("兑换比例不合适,请设置合适的兑换比例！");
        }
        // 计算店铺需要消耗的喜糖数量
        int candiesToExchange = videoCoinExchangeDTO.getExchangeVideoCoins();
        BigDecimal result = new BigDecimal(candiesToExchange).divide(new BigDecimal(exchangeRatio), 2, RoundingMode.HALF_UP);
        // 判断店铺原有的喜糖是否足够
        if (videoCoinExchange.getWeddingCandiesLast().compareTo(result) < 0) {
            throw new BaseException("店铺喜糖不足，无法兑换视频币！");
        }
        // 校验视频币数量的合法性
        if (videoCoinExchangeDTO.getExchangeVideoCoins() < 0) {
            throw new BaseException("视频币数量不合法,请输入合适的视频币数量！");
        }
        // 修改喜糖和视频币的数量
        int exchangedVideoCoins = videoCoinExchangeDTO.getExchangeVideoCoins();
        BigDecimal exchangeVideoCoins = new BigDecimal(exchangedVideoCoins);
        videoCoinExchange.setExchangeVideoCoins(exchangedVideoCoins);
        videoCoinExchange.setWeddingCandies(videoCoinExchange.getWeddingCandies().subtract(result));
        videoCoinExchange.setWeddingCandiesLast(videoCoinExchange.getWeddingCandies().add(result));
        videoCoinExchange.setRemark(videoCoinExchangeDTO.getRemark());
        videoCoinExchange.setExchangeTime(new Date());
        // 更新视频币兑换信息
        int rows = videoCoinExchangeMapper.updateById(videoCoinExchange);
        if (rows != 1) {
            throw new BaseException("视频币兑换失败，服务器忙，请稍后尝试！");
        }
        // 更新店铺表中的喜糖与视频币数据
        YxLiveShopEntity shop = shopMapper.selectById(videoCoinExchangeDTO.getExchangeShopId());
        shop.setWeddingCandies(videoCoinExchange.getWeddingCandies());
        shop.setLiveCoin(shop.getLiveCoin().add(exchangeVideoCoins));
        shop.setUpdateTime(new Date());
        rows = shopMapper.updateById(shop);
        if (rows != 1) {
            throw new BaseException("视频币兑换失败，服务器忙，请稍后尝试！");
        }
        // 视频币兑换生成视频币明细记录
        YxVideoCoinAddDTO addDTO = new YxVideoCoinAddDTO();
        addDTO.setExchangeRulesId(videoCoinExchange.getExchangeRulesId());
        addDTO.setType(1);
        addDTO.setDetail("2");
        videoCoinService.addVideoCoin(addDTO);
        // 将更新后的视频币兑换信息返回
        YxVideoCoinExchangeVO vo = new YxVideoCoinExchangeVO();
        BeanUtil.copyProperties(videoCoinExchange, vo);
        return vo;
    }

    /**
     * 添加视频币兑换数据
     * @param videoCoinExchangeDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void addVideoCoinExchange(YxVideoCoinExchangeDTO videoCoinExchangeDTO) {
        YxLiveShopEntity shopEntity = shopMapper.selectById(videoCoinExchangeDTO.getExchangeShopId());
        YxVideoCoinExchangeEntity videoCoinExchange = new YxVideoCoinExchangeEntity();
        BeanUtils.copyProperties(videoCoinExchangeDTO, videoCoinExchange);
        videoCoinExchange.setExchangeTime(new Date());
        videoCoinExchange.setWeddingCandiesLast(shopEntity.getWeddingCandies());
        videoCoinExchange.setWeddingCandies(shopEntity.getWeddingCandies());
        videoCoinExchange.setExchangeShopName(shopEntity.getShopName());
        int rows = videoCoinExchangeMapper.insert(videoCoinExchange);
        if (rows != 1) {
            throw new BaseException("添加视频币兑换数据失败，服务器忙，请稍后尝试！");
        }
    }

    /**
     * 根据店铺id查询视频币兑换数据
     * @param exchangeShopId
     * @return
     */
    @Override
    public YxVideoCoinExchangeVO getByExchangeShopId(Long exchangeShopId) {
        return videoCoinExchangeMapper.selectByExchangeShopId(exchangeShopId);
    }
}
